<?php
include 'config/config.php';

if (!isset($_POST['directory'])) return;

// upload thành công
if (!$_FILES['file']['error']) {
    // Ngan chan nguoi dung di tu ben ngoai thu muc goc
    $dir = realpath(BASEDIR . $_POST['directory']);
    $target = $dir . '/' . $_FILES['file']['name'];
    if (strpos($target, BASEDIR) !== 0) {
        echo '<script type="text/javascript">parent.uploadFailed();</script>'; // Ra bên ngoài iframe bởi sử dụng parent
        die();
    }

    // di chuyển file upload vao thư mục chi ra
    if (move_uploaded_file($_FILES['file']['tmp_name'], $target)) {
        echo '<script type="text/javascript">parent.refreshFilesList();</script>';
    } else {
        echo '<script type="text/javascript">parent.uploadFailed();</script>';
    }
} else {
    echo '<script type="text/javascript">parent.uploadFailed();</script>';
}
?>
